package com.wfit.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wfit.context.UserContext;
import com.wfit.dto.LoginFormDTO;
import com.wfit.dto.RegisterDto;
import com.wfit.dto.UserNameEmailDTO;
import com.wfit.po.Users;
import com.wfit.result.Result;
import com.wfit.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UsersController {
    private final UsersService usersService;

    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody  LoginFormDTO loginFormDTO){
        return Result.success(usersService.login(loginFormDTO));
    }
    @ApiOperation("用户注册接口")
    @PostMapping("/reg")
    public Result register(@RequestBody RegisterDto dto){
        return Result.success(usersService.register(dto));
    }
    @ApiOperation("获取用户信息")
    @GetMapping("/userinfo")
    public Result getUserInfo(){
        Users byId = usersService.getById(UserContext.getUser());
        return Result.success(byId);
    }
    @ApiOperation("获取用户信息2")
    @GetMapping("/userinfo2")
    public Result getUserInfo2(){
        Result result = usersService.getOneUser();
        return result;
    }
    @ApiOperation("用户名称和邮箱修改")
    @PutMapping("/updatename")
    public Result updateNameAndEmail(@RequestBody UserNameEmailDTO dto){
        Result result = usersService.updateNameAndEmail(dto);
        return result;
    }
    @Autowired
    private UsersService userService;
    @GetMapping("/page")
    public IPage<Users> findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String nickName,
                                @RequestParam(defaultValue = "") String email,
                                @RequestParam(defaultValue = "") String phonenumber){
        IPage<Users> page =new Page<>(pageNum,pageSize);
        QueryWrapper<Users> queryWrapper=new QueryWrapper<>();

        if (!"".equals(nickName)){
            queryWrapper.like("user_name",nickName);
        }
        if (!"".equals(email)){
            queryWrapper.like("email",email);
        }
        if (!"".equals(phonenumber)){
            queryWrapper.like("phonenumber",phonenumber);
        }
        queryWrapper.orderByDesc("id");
        return userService.page(new Page<>(pageNum, pageSize),queryWrapper);
    }

    @PostMapping
    public Result save(@RequestBody Users user) {
        return Result.success(userService.saveOrUpdate(user));
    }

    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return userService.removeById(id);
    }

    @PostMapping("/batch")
    public Boolean deleteBatch(@RequestBody List<Integer> ids) {
        return userService.removeByIds(ids);
    }
}
